{include file="public/_meta" /}

<body>
<div class="wrapper">

    {include file="public/_header" /}

    <!-- Sidebar -->
    {include file="public/_menu" /}
    <!-- End Sidebar -->

    <div class="main-panel">

        <div class="content">
            <div class="page-inner">

                <!-- 面包屑 -->
                {include file="public/_page_header" /}


                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <table class="layui-hide" id="demo" lay-filter="test"></table>
                        </div>
                    </div>
                </div>


                <script type="text/html" id="barTool">
                    <a href='{:url("download")}?time={{ d.time }}' class="layui-btn layui-btn-xs" title="下载"><i class="iconfont icon-flashlight_fill"></i> 下载</a>
                </script>


            </div>
        </div>

        {include file="public/_footer" /}


    </div>


</div>

{include file="public/_js" /}

</body>
<script>

</script>

<script src="__STATIC__/manage/assets/js/core/jquery.3.2.1.min.js"></script>
<script src="__STATIC__/layui/layui.js"></script>
<script>

    var $export = $("#export");

    layui.use(function () {
        // 得到需要的内置组件

        var table = layui.table; //表格

        var $ = layui.jquery;




        // 执行一个 table 实例
        table.render({
                elem: '#demo'
                , id: 'tableOne'
                , url: "{:url('Database/restoreDb')}" //数据接口（此处为静态数据，仅作演示）
                , title: '线索表'
                // , page: true //开启分页
                ,page:false
                , limit: 100
                // , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                // , defaultToolbar: ['filter']
                // , totalRow: true //开启合计行
                , response: {
                    statusCode: 0 //规定成功的状态码，默认：0
                    // , countName: 'total' //规定数据总数的字段名称，默认：count
                }
                , cols: [[ //表头
                { field: 'title', width: 250, title: '备份名称' },
                { field: 'date', width: 250, title: '备份时间' },
                { field: 'size', width: 120, title: '备份大小' },
                { field: 'part', width: 200, title: '卷数' },
                { field: 'compress', title: '压缩' },
                { field: '', width: 120, title: '状态', templet: '<div>-</div>' },
                { fixed: 'right', width: 180, title: '操作', toolbar: '#barTool' }
                ]],
                done: function (res, curr, count) {
                    $(".layui-table-main tr").each(function (index, val) {
                        $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                        $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                    });
                }
            }
        );

        // 头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                // case 'add':
                //     layer.msg('添加');
                //     break;
                // case 'update':
                //     if (data.length === 0) {
                //         layer.msg('请选择一行');
                //     } else if (data.length > 1) {
                //         layer.msg('只能同时编辑一个');
                //     } else {
                //         layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                //     }
                //     break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        var data = checkStatus.data;
                        var idArr=[];
                        $.each(data,function (index,val){
                            idArr.push(val.id)
                        })
                        $.ajax({
                            type: 'POST',
                            url: "{:url('message/deleteAll')}",
                            dataType: 'json',
                            data: {id: idArr},
                            success: function (data) {
                                if (data.code == 200) {
                                    window.location.reload();
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 2000});
                                }

                            },
                            error: function (data) {
                                layer.msg('网络错误,稍后再试...', {icon: 2, time: 2000});
                            }
                        });
                    }
                    break;
                case 'read':
                    if (data.length === 0) {
                        layer.msg('请选择需要备份的数据');
                    } else {

                        var data = checkStatus.data;
                        var a=[];
                        $.each(data,function (index,val){
                            a.push(val.name)
                        })


                        $export.parent().children().addClass("layui-btn-disabled");
                        $export.html("<i class='icon iconfont icon-fasong'></i>正在发送备份请求...");


                        $.ajax({
                            type: 'POST',
                            url: "{:url('database/export')}",
                            dataType: 'json',
                            data: {tables: a},
                            success: function (data) {
                                if (data.code) {
                                    tables = data.data.tables;
                                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                                    backup(data.data.tab);
                                    window.onbeforeunload = function() { return "正在备份数据库，请不要关闭！" }
                                } else {
                                    layer.msg(data.msg, { icon: 5 });
                                    $export.parent().children().removeClass("disabled");
                                    $export.html("立即备份");
                                }

                            },
                            error: function (data) {
                                layer.msg('网络错误,稍后再试...', {icon: 2, time: 2000});
                            }
                        });

                        return false;


                    }
                    break;
            }
            ;
        });

    });


    //提交备份
    $(document).on('click', '#export', function() {
        var checkStatus = table.checkStatus('currentTable');
        if (checkStatus.data.length <= 0) {
            layer.msg("请选择需要备份的数据",{icon: 2});
            return false;
        }
        var a = [];
        $(checkStatus.data).each(function(i, o) {
            a.push(o.name);
        });
        $export.parent().children().addClass("layui-btn-disabled");
        $export.html("<i class='icon iconfont icon-fasong'></i>正在发送备份请求...");
        $.post("{:url('export')}", { tables: a },
            function(data) {
                if (data.code) {
                    tables = data.data.tables;
                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                    backup(data.data.tab);
                    window.onbeforeunload = function() { return "正在备份数据库，请不要关闭！" }
                } else {
                    layer.msg(data.msg, { icon: 5 });
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            },
            "json"
        );
        return false;
    });

    function backup(tab, code) {
        code && showmsg(tab.id, "开始备份...(0%)");
        $.get("{:url('export')}", tab, function(data) {
            if (data.code) {
                showmsg(tab.id, data.msg);
                if (!$.isPlainObject(data.data.tab)) {
                    $export.parent().children().removeClass("layui-btn-disabled");
                    $export.html("备份完成，点击重新备份");
                    window.onbeforeunload = function() { return null }
                    return;
                }
                backup(data.data.tab, tab.id != data.data.tab.id);
            } else {
                layer.msg(data.msg, { icon: 5 });
                $export.parent().children().removeClass("disabled");
                $export.html("立即备份");
            }
        });
    }

    function showmsg(id, msg) {
        $('.layui-table').find('tr:eq('+id+') td:eq(6)').find('div').html(msg);
    }
</script>

</html>